package copyengine.ui.component.panel
{
	import copyengine.ui.component.CEUISprite;
	import copyengine.utils.GeneralUtils;
	import copyengine.utils.tick.GlobalTick;

	import flash.display.Sprite;
	import flash.events.Event;

	public class CEVPanelBasic extends CEUISprite
	{
		private var allSubElementVector:Vector.<CEPanelSubElement>;
		private var isLayoutPanelInNextTick:Boolean;

		/**
		 *子Element和子Element之间的垂直间距
		 */
		private var subElementVGap:int;

		/**
		 *<b>所有子Element的父容器</b>
		 * </br></br>层级结构是:
		 * panel自己(外层访问到的CEUISprite)->panelContainer->allSubElement</br>
		 * 这样做的目的是让子类有机会调整panelContainer的位置及显示,因为父类的 layoutSubElement
		 * 只是将Element按顺序排好(以0,0点对齐),子类也许会调整位置是其在外部访问时候为居中显示
		 */
		protected var panelContainer:Sprite;

		/**
		 *<b>层级关系在panelContainer后面的Mc</b>
		 */
		protected var panelBgContainer:Sprite;

		private var vPanelRealHeight:int


		/**
		 *
		 * @param _vGap		Element和Element之间的垂直间隔
		 *
		 */
		public function CEVPanelBasic(_vGap:int = 0)
		{
			super();
			subElementVGap = _vGap;
			initialize();
		}

		private function initialize():void
		{
			allSubElementVector = new Vector.<CEPanelSubElement>();

			panelContainer = new Sprite();
			panelBgContainer = new Sprite();
			this.addChild(panelBgContainer);
			this.addChild(panelContainer);

			doInitialize();
		}

		protected function doInitialize():void
		{
		}

		override protected function doDispose():void
		{
			onPanelDispose();
			GlobalTick.instance.removeTickNodeByFunction(doLayoutSubElement);
			GeneralUtils.removeTargetFromParent(panelContainer);
			GeneralUtils.removeTargetFromParent(panelBgContainer);
		}

		protected function onPanelDispose():void
		{
		}


		/**
		 * <b>给当前Panel面板添加ChildElement</b>
		 * <li>添加的ChildElement会按照添加的先后排序(从上到下或者从左到右)
		 * <li>在该函数调用后的下一帧会进行layout(根据layout不同排列不同)
		 */
		public final function addSubElement(_element:CEPanelSubElement):void
		{
			_element.initialize();
			_element.setElementResizedCallBack(layoutSubElement);
			allSubElementVector.push(_element);
			panelContainer.addChild(_element.getElementContainer());
			layoutSubElement();
		}

		protected final function layoutSubElement():void
		{
			if (!isLayoutPanelInNextTick)
			{
				isLayoutPanelInNextTick = true;
				GlobalTick.instance.callLaterAfterTickCount(doLayoutSubElement);
			}
		}

		private function doLayoutSubElement():void
		{
			isLayoutPanelInNextTick = false;
			vPanelRealHeight = 0;
			for (var index:int = 0; index < allSubElementVector.length; index++)
			{
				//所有Element居左对齐
				allSubElementVector[index].getElementContainer().x = 0;
				allSubElementVector[index].getElementContainer().y = vPanelRealHeight;
				vPanelRealHeight += allSubElementVector[index].getElementContainer().height;
				vPanelRealHeight += subElementVGap;
			}
			onFinishedLaytoutSubElement();
		}

		/**
		 * <b>在父类Layout完所有Element后回调子类处理</b>
		 */
		protected function onFinishedLaytoutSubElement():void
		{
		}

		/**
		 * <b>返回VPanel实际的高度</b>
		 * </br></br>因为ScrollableVPanel会设置VPanel的ScrollRect,导致VPanel的
		 * 大小为ScrollRect后的大小,但是外部Scrollbar需要一个实际的大小确定滚动最大值
		 */
		protected function getVPanelRealHeight():int
		{
			return vPanelRealHeight;
		}


	}
}
